<?php

/**
 * @file SendEmail
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Amz_Resource_SendEmailSolicitud {

    public function sendEmail($id_solicitud = 0, $id_users = null, $type = '') {
        // list emails for send
        $toMails = $this->getEmailSolicitud($id_users);
        $solicitud = $this->getDataSolicitud($id_solicitud);

        if ($solicitud != null) {

            $smtpServer = 'mail.velcro-lab.com';
            $username = 'admpro@velcro-lab.com';
            $password = 'admpro';


            $config = array('ssl' => 'tls',
                'auth' => 'login',
                'username' => $username,
                'password' => $password);

            $transport = new Zend_Mail_Transport_Smtp($smtpServer, $config);

            // Set From & Reply-To address and name for all emails to send.
            Zend_Mail::setDefaultFrom('admpro@velcro-lab.com', 'Seguimiento Proyectos');
            // create view object
            $html = new Zend_View();
            $html->setScriptPath(APPLICATION_PATH . '/modules/solicitud/views/emails/');
            $html->assign('solicitud', $solicitud);
            
            $request = Zend_Controller_Front::getInstance()->getRequest();
            //$url = $request->getScheme() . '://' . $request->getHttpHost() . '';
            $url = $request->getScheme() . '://' . $request->getHttpHost() . '/capataz';
            // Loop through messages
            foreach ($toMails as $to) {
                $html->assign('name', $to['name']);
                
                switch ($type) {
                    case 'ADD_SOL':
                        $html->assign('link', $url . '/solicitud/solicitud/solicitud-view-to/id/' . $id_solicitud);
                        $bodyText = $html->render('new-solicitud.phtml');
                        $subject = 'Solicitud';
                        break;
                    case 'DEL_SOL':
                        $html->assign('link','');
                        $bodyText = $html->render('delete-solicitud.phtml');
                        $subject = 'Solicitud Cancelada';
                        break;
                    case 'APRO_SOL':
                        $html->assign('link', $url . '/solicitud/solicitud/solicitud-view-from/id/' . $id_solicitud);
                        $bodyText = $html->render('aprobada-solicitud.phtml');
                        $subject = 'Solicitud Aprobada';
                        break;
                }
                // Email config send
                $mail = new Zend_Mail();
                $mail->addTo($to['email'], $to['name']);
                $mail->setSubject($subject);
                $mail->setBodyHtml($bodyText);
                $mail->send($transport);
            }

            // Reset defaults
            Zend_Mail::clearDefaultFrom();
        }
    }

    private function getEmailSolicitud($id_users = null) {

        $usersDB = new User_Model_DbTable_User();
        $toMails = array();

        foreach ($id_users as $val) {
            if ($val != null && $val != 0) {
                $rowUser = $usersDB->fetchRow('id=' . $val);
                $names = $rowUser->nombre . ' ' . $rowUser->apellidos;
                $toMails[] = array('name' => $names, 'email' => $rowUser->email);
            }
        }
        return $toMails;
    }

    /*
     * Get data orden Id only a row
     */

    private function getDataSolicitud($id_solicitud = 0) {
        $solicitud = new Solicitud_Model_DbTable_Solicitud();

        $row = $solicitud->fetchRow('id=' . $id_solicitud);

        if ($row != null)
            return $row;
        else
            return null;
    }

}

?>
